package com.ikun.admin.modules.gen.iservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikun.admin.modules.gen.entity.CodeConfig;
import com.ikun.admin.modules.gen.entity.Table;
import com.ikun.admin.modules.gen.iservice.ICodeConfigService;
import com.ikun.admin.modules.gen.service.CodeConfigService;
import com.ikun.admin.modules.gen.service.TableService;
import com.ikun.admin.modules.gen.vo.CodeConfigCommonVo;
import com.ikun.admin.modules.gen.vo.CodeConfigDictVo;
import com.ikun.common.exception.MyException;
import com.ikun.common.result.Result;
import com.ikun.common.search.PageData;
import com.ikun.common.search.PageResult;
import com.ikun.common.search.build.QueryBuilder;
import com.ikun.common.utils.DataCopy;
import com.ikun.common.validate.validator.ValidateExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName ICodeConfigServiceImpl
 * @Description:
 * @CreateDate: 2024/4/16 0:53
 */
@Service
public class ICodeConfigServiceImpl implements ICodeConfigService {
    @Autowired
    private CodeConfigService codeConfigService;

    @Autowired
    private TableService tableService;

    @Override
    @Transactional
    public Result<String> codeConfigAddOrUpdate(CodeConfigCommonVo codeConfigCommonVo) {
        // 参数校验
        ValidateExecutor.execute(codeConfigCommonVo, null);
        if (Objects.isNull(codeConfigCommonVo.getId())) {
            //新增
            CodeConfig codeConfig = DataCopy.copyToObj(codeConfigCommonVo, CodeConfig.class);
            codeConfigService.save(codeConfig);
        } else {
            //编辑
            codeConfigService.update(new UpdateWrapper<CodeConfig>().lambda()
                    .eq(CodeConfig::getId, codeConfigCommonVo.getId())
                    .set(CodeConfig::getIdType, codeConfigCommonVo.getIdType())
                    .set(CodeConfig::getModule, codeConfigCommonVo.getModule())
                    .set(CodeConfig::getRootPath, codeConfigCommonVo.getRootPath()));

        }
        return new Result<String>().success();
    }

    @Override
    public Result<PageResult<CodeConfigCommonVo>> codeConfigList(PageData<CodeConfigCommonVo> pageData) {
        PageData.PageParam pageParam = pageData.getPageParam();
        Integer current = pageParam.getCurrent();
        Integer size = pageParam.getSize();
        CodeConfigCommonVo queryVo = pageData.getCondition();
        IPage<CodeConfig> page = new Page<CodeConfig>().setSize(size).setCurrent(current);

        QueryWrapper<CodeConfig> codeConfigQueryWrapper = new QueryWrapper<>();
        Boolean build = QueryBuilder.build(codeConfigQueryWrapper, queryVo);
        if (!build) {
            return new Result<PageResult<CodeConfigCommonVo>>().success(PageResult.emptyPageResult());
        }
        codeConfigService.page(page, codeConfigQueryWrapper);
        if (page.getRecords().size() > 0) {
            List<CodeConfigCommonVo> resultList = DataCopy.copyToListObj(page.getRecords(), CodeConfigCommonVo.class);
            return new Result<PageResult<CodeConfigCommonVo>>().success(PageResult.getInstance(page.getTotal(), resultList));
        } else {
            return new Result<PageResult<CodeConfigCommonVo>>().success(PageResult.emptyPageResult());
        }
    }

    @Override
    @Transactional
    public Result<String> codeConfigDelete(Long id) {

        // 查询配置下是否有表
        List<Table> list = tableService.list(new QueryWrapper<Table>().lambda().eq(Table::getCodeConfigId, id));
        if (list.size() > 0) {
            List<String> tableNameList = list.stream().map(item -> item.getTableName()).collect(Collectors.toList());
            throw new MyException("不能删除该配置，因为该配置下还有:" + tableNameList + "等张表");
        }
        codeConfigService.removeById(id);
        return new Result<String>().success();
    }

    @Override
    public Result<CodeConfigCommonVo> codeConfigDetail(Long id) {
        CodeConfig detail = codeConfigService.getOne(new QueryWrapper<CodeConfig>().lambda().eq(CodeConfig::getId, id));
        CodeConfigCommonVo codeConfigCommonVo = DataCopy.copyToObj(detail, CodeConfigCommonVo.class);
        return new Result<CodeConfigCommonVo>().success(codeConfigCommonVo);
    }

    @Override
    public Result<List<CodeConfigDictVo>> codeConfigDict() {
        List<CodeConfig> codeConfigList = codeConfigService.list(new QueryWrapper<>());
        List<CodeConfigDictVo> codeConfigDictVoList = DataCopy.copyToListObj(codeConfigList, CodeConfigDictVo.class);
        return new Result<List<CodeConfigDictVo>>().success(codeConfigDictVoList);
    }
}
